﻿using CL_SQL;
using CL_SQL.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace StudentLoan.Controllers
{
    public class HomeController : Controller
    {
        LoginBLL lb = new LoginBLL();

        public ActionResult Index()
        {
            ////判断session里面是否有值
            var a = (UserLogin)HttpContext.Session["CurrUser"];

            if (a != null)
            {
                ViewBag.UserId = a.UserId;
                ViewBag.UserAccount = a.UserAccount;
                ViewBag.UserPassword = a.UserPassword;
                ViewBag.UserName = a.UserName;
                ViewBag.LastModifyDate = a.LastModifyDate;
                ViewBag.LastModifyUser = a.LastModifyUser;
                return View();
            }
            else
            {
                //return RedirectToAction("Login", "Home");
                return View();
            }

            
        }
        public ActionResult Login()
        {
            return View();
        }
        public void ChangePwd()
        {
            string UserAccount = Request.Params["UserAccount"];
            string OldUserPassword = Request.Params[" OldUserPassword"];
            string NewUserPassword = Request.Params["NewUserPassword"];
            if (lb.ChangePwd(UserAccount, OldUserPassword, NewUserPassword) )
            {
                HttpContext.Response.Write(0);
                HttpContext.Response.End();
            } else
            {
                HttpContext.Response.Write(1);
                HttpContext.Response.End();
            }
        }
        public ActionResult LoginCheck()
        {
            string UserAccount =  Request.Form["txtUserAccount"];
            string pwd = Request.Form["txtUserPassword"];
            UserLogin userLogin = new UserLogin();
            userLogin = lb.LoginCheck(UserAccount, pwd);
            if (userLogin != null)
            {
                HttpContext.Session["CurrUser"] = userLogin;
               return RedirectToAction("Index", "Home");
            }
            else
            {
                return RedirectToAction("Login","Home");
            }
            
        }

        public ActionResult exit()
        {
            HttpContext.Session["CurrUser"] = null;
            return RedirectToAction("Login", "Home");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}